package com.xigua.字符串;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/2123:03
 */
public class _3_无重复字符的最长子串 {


    public static void main(String[] args) {
        lengthOfLongestSubstring("abcabcbb");
    }

    public static  int lengthOfLongestSubstring(String s) {
        int max = 0;
        int preMaxLength = 0;

        Map<Character,Integer> hashMap = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
           int j = hashMap.getOrDefault(chars[i],-1);
           preMaxLength = preMaxLength < (i - j) ? preMaxLength+1 : i-j;
           hashMap.put(chars[i],i);
           max = Math.max(preMaxLength,max);
        }
        return max;
    }

}
